package models;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import exceptions.DuplicateUrlKeyException;

import play.data.validation.MaxSize;
import play.data.validation.Required;
import play.db.jpa.JPABase;
import play.db.jpa.Model;

/**
 * 产品实体
 * 
 * @author popoer
 *
 */
@Entity
@Table(name="product")
public class Product extends Model {
    
	@ManyToMany(cascade=CascadeType.PERSIST)
	public List<ProductType> productTypes;
	
	@Required
	public String name;
	
	@Column(name="url_key",unique=true,nullable=false)
	public String urlKey;
	
	@MaxSize(1024)
	public String summary;
	
	@Lob
	@MaxSize(30000)
	public String overview;
	
	@Lob
	@MaxSize(30000)
	public String specifications;
	
	public String brochure;
	
	@Column(name="homepage_big_image")
	public String homepageBigImage;
	
	@Column(name="homepage_small_image")
	public String homepageSmallImage;
	
	@Column(name="detail_image")
	public String detailImage;
	
	@ElementCollection
	public List<String> images;
	
	public int priority;
	
	@Column(name="show_in_homepage")
	public boolean showInHomepage;
	
	@Column(name="create_time")
	public Date createTime = new Date();
	
	@Column(name="update_time")
	public Date updateTime = new Date();
	
	@Column(name="dentalseller_url",length=256)
	public String dentalsellerUrl;
	
	@Column(name="amazon_url",length=256)
	public String amazonUrl;
	
	@Column(name="ebay_url",length=256)
	public String ebayUrl;
	
	@Override
	public String toString() {
		return this.name;
	}
	
	@ManyToOne
	public User creator = new User();
	
	@ManyToOne
	public User modifier = new User();
	
	@Override
	public void _save() {
		Product product;
		if(this.id==null){
			product = findByUrlKey(this.urlKey);
		}else{
			product = Product.find("from Product where urlKey=? and id<>?", this.urlKey,this.id).first();
		}
		if(product!=null){
			throw new DuplicateUrlKeyException();
		}
		if(this.creator!=null){
			this.creator = User.findByUsername(this.creator.username);
		}
		if(this.modifier!=null){
			this.modifier = User.findByUsername(this.modifier.username);
		}
		this.updateTime = new Date();
		super._save();
	}
	
	public static Product findByUrlKey(String urlKey){
		return Product.find("from Product where urlKey=?", urlKey).first();
	}
	
	public static List<Product> findProducts(Long productTypeId,int page,int pageSize){
		List<Product> products;
		if(productTypeId!=null){
			products = Product.find("select p from Product p inner join p.productTypes as t where t in (select t from ProductType t where id=?) order by p.priority", productTypeId).fetch(page, pageSize);
		}else{
			products = Product.find("from Product order by priority").fetch(page,pageSize);
		}
		return products;
	}
	
	public static long count(Long productTypeId){
		if(productTypeId!=null){
			return count("from Product p inner join p.productTypes as t where t in (select t from ProductType t where id=?)", productTypeId);
		}else{
			return count();
		}
	}
	
	/**
	 * 首页推荐产品
	 * @param amount
	 * @return
	 */
	public static List<Product> findHomePageProducts(int amount){
		return find("from Product where showInHomepage=true order by priority").fetch(amount);
	}
	
}
